<template>
   <div class="tag-container">
      <el-card style="margin:10px">
  
        <el-breadcrumb slot="header" v-if="subject.id && subject.name">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>

        <el-row>
        <el-col :span="18">
          <el-form label-width="80px" :inline="true" size="mini">
            <el-form-item label="标签名称" >
              <el-input style="200px" v-model="requestParams.tagName"></el-input>
            </el-form-item>
            <el-form-item label="状态" >
              <el-select style="200px" v-model="requestParams.state" placeholder="请选择">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clearTaginfo">清除</el-button>
              <el-button type="primary" @click="getTagList()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="6" style="text-align:right">
          <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
          <el-button icon="el-icon-edit" type="success" size="mini" @click="openDialog()">新增学科</el-button>
        </el-col>
        
        <el-alert
          :title="`数据一共${counts}条`"
          type="info"
          show-icon
          :closable="false"
          style="margin-bottom: 10px">
        </el-alert>
        </el-row>

        <el-table :data="TagList" style="width: 100%" >
          <el-table-column label="序号" type="index" width="80px"></el-table-column>
          <el-table-column label="所属学科" prop="subjectName"></el-table-column>
          <el-table-column label="标签名称" prop="tagName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column label="创建日期">
            <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
          </el-table-column>
          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-button type="text" @click="toggleState(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</el-button>
              <el-button type="text" @click="openDialog(scope.row)" :disabled="scope.row.state===1">修改</el-button>
              <el-button type="text" @click="delTag(scope.row)" :disabled="scope.row.state===1||scope.row.totals > 0">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination  
          style="margin-top:20px;text-align:right"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="requestParams.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="counts">
        </el-pagination>
      </el-card>
      <tags-add ref="tagsadd" :currTags="currTags" @updateList='updateList()'></tags-add>
   </div>
</template>

<script>
import {list as taglist, changeState, remove} from '@/api/hmmm/tags'
import tagsAdd from '../components/tags-add.vue'
export default {
   components: { tagsAdd },
   name: 'TagsList',
   data() {
     return {
      TagList:[],
      currTags:{},
      counts: 0,
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
     }
   },
   created() {
     this.getTagList()
   },
   methods: {
      async getTagList() {
        this.requestParams.subjectID = this.subject.id || null
        const { data } = await taglist(this.requestParams)
        this.TagList = data.items
        this.counts = data.counts
      },
      clearTaginfo() {
        this.requestParams.tagName = ''
        this.requestParams.state = ''
      },
      handleSizeChange(newNum) {
        this.requestParams.page = 1
        this.requestParams.pagesize = newNum
        this.getTagList()
      },
      handleCurrentChange(newPage) {
        this.requestParams.page = newPage
        this.getTagList()
      },
      openDialog (tag = {}) {
        this.currTags = tag
        this.$nextTick(() => {
          this.$refs.tagsadd.open()
        })
      },
      async toggleState (tag) {
        await changeState({
          id: tag.id,
          state: tag.state === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        tag.state = tag.state === 1 ? 0 : 1
      },
      async delTag (tag) {
        await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await remove(tag)
        this.$message.success('删除成功')
        this.getTagList()
      },
      updateList () {
        if (!this.currTags.id) {
          this.requestParams.page = 1
        }
        this.getTagList()
        console.log(this.currTags);
      }
   },
   computed: {
    subject () {
      return this.$route.query || {}
    }
  },
   watch: {
    '$route.query': function () {
      this.getTagList()
    }
  },

}
</script>

<style lang='less' scoped>
</style>
